package cn.flying.cloud.system.api.feign;

import javax.annotation.Resource;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.flying.cloud.base.common.bo.UserInfo;
import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.system.api.UserInfoServiceApi;
import cn.flying.cloud.system.log.entity.SysLoginLog;
import cn.flying.cloud.system.log.service.SysLoginLogService;
import cn.flying.cloud.system.model.LoginLog;
import cn.flying.cloud.system.user.service.SysUserInfoService;

/**
 * 用户认证服务接口
 *
 * @author: admin
 * @date: 2023年04月19日 16:27
 * @version: 1.0
 */
@RestController
@RequestMapping("/system/userInfo/api")
public class UserInfoApiController implements UserInfoServiceApi {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysUserInfoService sysUserInfoService;
    @Resource
    private SysLoginLogService sysLoginLogService;

    /**
     * 查询用户认证信息
     *
     * @param username
     * @param type
     * @return
     */
//    @RequestMapping("/authInfo")
    @Override
    public UserInfo getUserAuthInfo(String username, String type) {
        return sysUserInfoService.getUserAuthInfo(username, type);
    }

    /**
     * 同步注册用户信息
     *
     * @param authInfo
     * @return
     */
//    @RequestMapping("/register")
    @Override
    public String register(UserInfo authInfo) {
        return sysUserInfoService.register(authInfo);
    }

    /**
     * 同步登录日志
     *
     * @param log@return
     */
    @Override
    public Rt<?> loginLog(LoginLog log) {
        SysLoginLog loginLog = new SysLoginLog();
        Ft.BeanCopier.copy(log, loginLog);
        if (Objects.isNull(loginLog.getId())) {
            // 新增
            sysLoginLogService.createObj(loginLog);
        } else {
            // 更新
            sysLoginLogService.updateObj(loginLog);
        }
        return Rt.success("保存成功！");
    }
}
